<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin_product
 *
 * @author hoabui
 */
class admin_product extends CI_Controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        set_validation_error_delimiter(); //Ckeditor's configuration
    }

    function index() {

        $data['subtitle'] = 'Danh sách tất cả sản phẩm';
        $data['site'] = 'tp-admin/site/product/product-listview';

        $page = $this->uri->segment(4);
        if ($page <= 1)
            $page = 0;
        else
            $page = ($page - 1) * PAGE_SIZE;
        $product = $this->product_model->getAllProducts_Admin($page);
        $data['list'] = $product['list'];
        $total = $product['total'];
        $link = ADMIN_PAGE_PRODUCT;
        set_paging($link, $total, 4);
        $this->load->view('tp-admin/template', $data);
    }

    function new_products() {

        $data['subtitle'] = 'Danh sách sản phẩm mới';
        $data['site'] = 'tp-admin/site/product/product-listview';

        $page = $this->uri->segment(4);
        if ($page <= 1)
            $page = 0;
        else
            $page = ($page - 1) * PAGE_SIZE;
        $product = $this->product_model->getNewProducts_Admin($page);
        $data['list'] = $product['list'];
        $total = $product['total'];
        $link = ADMIN_PAGE_PRODUCT;
        set_paging($link, $total, 4);
        $this->load->view('tp-admin/template', $data);
    }

    function old_products() {

        $data['subtitle'] = 'Danh sách sản phẩm cũ';
        $data['site'] = 'tp-admin/site/product/product-listview';

        $page = $this->uri->segment(4);
        if ($page <= 1)
            $page = 0;
        else
            $page = ($page - 1) * PAGE_SIZE;
        $product = $this->product_model->getOldProducts_Admin($page);
        $data['list'] = $product['list'];
        $total = $product['total'];
        $link = ADMIN_PAGE_PRODUCT;
        set_paging($link, $total, 4);
        $this->load->view('tp-admin/template', $data);
    }

    function add_product() {

        $data['categories'] = $this->category_model->getAllCategories();
        $data['subtitle'] = 'Thêm sản phẩm mới';

        $data['site'] = 'tp-admin/site/product/product-add';

        if ($this->input->post('submit') && $this->check_validation()) {

            $config['upload_path'] = './uploaded/';
            $config['allowed_types'] = 'jpg|jpeg|gif|png';

            $this->load->library('upload', $config);
            if (!$this->upload->do_upload()) {

                $this->session->set_userdata(array('error_msg' => $this->upload->display_errors()));
                redirect(current_url());
            }

            $config1['source_image'] = $this->upload->upload_path . $this->upload->file_name;
            $config1['maintain_ratio'] = FALSE;
            $config1['width'] = 300;
            $config1['height'] = 300;

            $this->load->library('image_lib', $config1);

            if (!$this->image_lib->resize()) {
                $this->session->set_flashdata('message', $this->image_lib->display_errors('<p class="error">', '</p>'));
            }


            $info = $this->post_input();
            $info['product_id'] = generate_number_code(8);
            $info['product_name_segment'] = utf8_to_ascii($info['product_name'], '-');
            $info['product_tag'] = replace_space_to_dash($info['product_name'], ', ') . ', ' . replace_space_to_dash($info['product_name_segment'], ', ');
            $info['product_is_deleted'] = 0;
            $info['product_date'] = date('Y-m-d H:i:s');
            $info['product_viewer'] = 0;
            $info['product_rate'] = 0;
            $file_data = $this->upload->data();
            $image_info = array(
                'image_id' => generate_number_code(8),
                'product_id' => $info['product_id'],
                'image_url' => $file_data['file_name'],
                'image_is_delete' => 0,
                'image_display_state' => 1,
                'image_is_default' => 1,
                'image_description' => $info['product_name']
            );
            if ($info['product_is_old'] == "" && $info['product_is_new'] == "") {
                $info['product_is_new'] = 1;
            } else {

                if ($info['product_is_old'] != "")
                    $info['product_is_old'] = 1;
                else
                    $info['product_is_old'] = 0;

                if ($info['product_is_new'] != "") {
                    $info['product_is_new'] = 1;
                }
                else
                    $info['product_is_new'] = 0;
            }
            if ($this->product_model->addProduct($info)) {

                $this->product_image_model->addImage($image_info);
                $this->session->set_userdata(array('success_msg' => 'Thêm sản phẩm thành công'));
                redirect(current_url());
            } else {
                $this->session->set_userdata(array('error_msg' => 'Thêm sản phẩm thất bại'));
                redirect(current_url());
            }
        }

        $this->load->view('tp-admin/template', $data);
    }

    function update_product() {

        $id = $this->uri->segment(4);
        $data['categories'] = $this->category_model->getAllCategories();
        $data['subtitle'] = 'Cập nhật sản phẩm';
        $data['product_info'] = $this->product_model->getProductByID($id);

        $data['site'] = 'tp-admin/site/product/product-update';

        if ($this->input->post('submit') && $this->check_validation()) {

            $info = $this->post_input();

            $info['product_name_segment'] = utf8_to_ascii($info['product_name'], '-');
            $info['product_tag'] = replace_space_to_dash($info['product_name'], ', ') . ', ' . replace_space_to_dash($info['product_name_segment'], ', ');
            $info['product_is_deleted'] = 0;
            $info['product_date'] = date('Y-m-d H:i:s');
            $info['product_viewer'] = 0;
            $info['product_rate'] = 0;


            if ($info['product_is_old'] == "" && $info['product_is_new'] == "") {
                $info['product_is_new'] = 1;
            } else {

                if ($info['product_is_old'] != "")
                    $info['product_is_old'] = 1;
                else
                    $info['product_is_old'] = 0;

                if ($info['product_is_new'] != "") {
                    $info['product_is_new'] = 1;
                }
                else
                    $info['product_is_new'] = 0;
            }
            if ($this->product_model->updateProduct($info, $id)) {


                $this->session->set_userdata(array('success_msg' => 'Cập nhật sản phẩm thành công'));
                redirect(current_url());
            } else {
                $this->session->set_userdata(array('error_msg' => 'Cập nhật sản phẩm thất bại'));
                redirect(current_url());
            }
        }

        $this->load->view('tp-admin/template', $data);
    }

    function delete_product() {

        $id = $this->uri->segment(4);

        $data['site'] = 'tp-admin/site/product/product-update';

        $this->product_model->deleteProduct($id);

        redirect(HOME_PAGE . ADMIN_PAGE . "admin_product");
    }

    function post_input() {
        $info = array(
            'product_name' => $this->input->post('product_name'),
            'product_company_price' => $this->input->post('product_company_price'),
            'product_tan_phat_price' => $this->input->post('product_tan_phat_price'),
            'product_quantity' => $this->input->post('product_quantity'),
            'product_guarantee_time' => $this->input->post('product_guarantee_time'),
            'product_accessories' => $this->input->post('product_accessory'),
            'product_is_old' => $this->input->post('product_old'),
            'product_is_new' => $this->input->post('product_new'),
            'product_is_display' => $this->input->post('product_display_state'),
            'product_summary' => $this->input->post('product_summary'),
            'product_detail' => $this->input->post('product_detail'),
            'category_id' => $this->input->post('product_category')
        );
        return $info;
    }

    function add_images() {
        $id = $this->uri->segment(4);
        $data['product_info'] = $this->product_model->getProductByID($id);
        $data['product_images'] = $this->product_image_model->getImagesByProduct1($id);
        $data['subtitle'] = 'Thêm ảnh cho sản phẩm ' . $data['product_info']->product_name;
        $data['site'] = 'tp-admin/site/product/add-images';
        if ($this->input->post('submit')) {
            $config['upload_path'] = './uploaded/';
            $config['allowed_types'] = 'jpg|jpeg|gif|png';

            $this->load->library('upload', $config);
            if (!$this->upload->do_upload()) {

                $this->session->set_userdata(array('error_msg' => $this->upload->display_errors()));
                redirect(current_url());
            }
            $config1['source_image'] = $this->upload->upload_path . $this->upload->file_name;
            $config1['maintain_ratio'] = FALSE;
            $config1['width'] = 300;
            $config1['height'] = 300;

            $this->load->library('image_lib', $config1);

            if (!$this->image_lib->resize()) {
                $this->session->set_flashdata('error_msg', $this->image_lib->display_errors('<p class="error">', '</p>'));
                redirect(current_url());
            }

            $file_data = $this->upload->data();
            $image_info = array(
                'image_id' => generate_number_code(8),
                'product_id' => $id,
                'image_url' => $file_data['file_name'],
                'image_is_delete' => 0,
                'image_display_state' => 1,
                'image_is_default' => 0,
                'image_description' => $data['product_info']->product_name
            );
            if (!$this->product_image_model->addImage($image_info)) {
                $this->session->set_userdata(array('error_msg' => 'Thêm hình ảnh thất bại'));
                redirect(current_url());
            } else {
                $this->session->set_userdata(array('success_msg' => 'Thêm hình ảnh thành công'));
                redirect(current_url());
            }
        }
        $this->load->view('tp-admin/template', $data);
    }

    function delete_image() {
        $id = $this->uri->segment(4);
        $this->product_image_model->deleteImage($id);
        $data['product_info'] = $this->product_model->getProductByID($id);
        $data['product_images'] = $this->product_image_model->getImagesByProduct1($id);
        $data['subtitle'] = 'Thêm ảnh cho sản phẩm ' . $data['product_info']->product_name;
        $data['site'] = 'tp-admin/site/product/add-images';
        $this->load->view('tp-admin/template', $data);
    }

    function check_validation() {
        $check_validation = array(
            array(
                'field' => 'product_name',
                'label' => 'Tên sản phẩm',
                'rules' => 'trim|required'
            ), array(
                'field' => 'product_company_price',
                'label' => 'Giá công ty',
                'rules' => 'trim|required|integer|greater_than[0]'
            ),
            array(
                'field' => 'product_tan_phat_price',
                'label' => 'Giá tấn phát',
                'rules' => 'trim|required|integer|greater_than[0]'
            ),
            array(
                'field' => 'product_tan_phat_price',
                'label' => 'Giá tấn phát',
                'rules' => 'trim|required|integer|greater_than[0]'
            ),
            array(
                'field' => 'product_quantity',
                'label' => 'Số lượng',
                'rules' => 'trim|required|integer'
            ),
            array(
                'field' => 'product_guarantee_time',
                'label' => 'Thời gian bảo hành',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'product_accessory',
                'label' => 'Phụ kiện',
                'rules' => 'trim|required'
            )
        );
        $this->form_validation->set_rules($check_validation);
        return $this->form_validation->run();
    }

}

?>
